package leetcode_day._2021._202109._0110;

import java.util.Arrays;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/9/1 9:12
 * 比较版本号
 * 算法：双指针  字符串
 * 使用双指针，可以将空间空间复杂度从 n 减少到 1
 */
public class _01_165 {
    public static void main(String[] args) {
        String s = "1.1";
        System.out.println(Arrays.toString(s.split("\\.")));
    }

    public int compareVersion(String version1, String version2) {
        final String[] s1 = version1.split("\\.");
        final String[] s2 = version2.split("\\.");
        int len1 = s1.length, len2 = s2.length;
        int len = Math.max(s1.length, s2.length);
        for (int i = 0; i < len; i++) {
            final int v1 = i < len1 ? Integer.parseInt(s1[i]) : 0;
            final int v2 = i < len2 ? Integer.parseInt(s2[i]) : 0;
            if (v1 != v2) return v1 > v2 ? 1 : -1;
        }
        return 0;
    }

    public int compareVersion_doublePoint(String version1, String version2) {
        int len1 = version1.length(), len2 = version2.length();
        int i = 0, j = 0;
        while (i < len1 || j < len2) {
            int x = 0;
            for (; i < len1 && version1.charAt(i) != '.'; i++) x = x * 10 + version1.charAt(i) - '0';
            ++i;
            int y = 0;
            for (; j < len2 && version2.charAt(j) != '.'; j++) y = y * 10 + version2.charAt(j) - '0';
            ++j;
            if (x != y) return x > y ? 1 : -1;
        }
        return 0;
    }

}
